/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTMP_CLIENT_H
#define MK_RTMP_CLIENT_H
#include "MkTcp/MkTcpClient.h"
#include "MkRtmpParam.h"

class CMkAmf;
class MkApi CMkRtmpClient : public CMkTcpClient, public CMkRtmpParam
{
public:
    CMkRtmpClient(CMkEvent* pEvent, CMkTimer* pTimer, const MkEventRemovedCallback& RemoveCb);
    virtual ~CMkRtmpClient();

    Uint32 ConnectUrl(const MkString& Url);

    virtual Uint32 OnHandSharked() = 0;
    Uint32 SendConnect(BOOL bSender);
    Uint32 SendWindownAcknowledgementSize();
    Uint32 SendCreateStream();
    Uint32 SendGetStreamLength();
    Uint32 SendPlay(const MkString& StreamFormat = "", int StrartSeconds = 0, int Duration = 0, BOOL bReset = FALSE);
    Uint32 SendPlay2(Uint32 StartTime, const MkString& OldStreamName, Uint32 Duration, const MkString& Transition);
    Uint32 SendAcknowledgement();
    Uint32 SendReleaseStream();
    Uint32 SendFcPublish();
    Uint32 SendChunkSize(Uint32 ChunkSize);
    Uint32 SendPublish();
    Uint32 SendSeek(Uint32 MilliSeconds);
    Uint32 SendPause(BOOL bPause, Uint32 MilliSeconds);
protected:
    Uint32 OnRecv() override;
    Uint32 OnClose() override;
private:
    Uint32 SendHandSharkeC0C1();
    Uint32 SendHandSharkeC2();

    Uint32 SendPong();
    Uint32 SendUsherToken(const MkString& Token);
    Uint32 SendFCSubscribe(const MkString& SubscribePath);

    Uint32 SendDeleteStream();
    Uint32 SendReceiveVideo(BOOL bSend);
    Uint32 SendReceiveAudio(BOOL bSend);
    Uint32 SendPlaylist();

    Uint32 SendCheckBWResult(Uint32 TransactionId);
    Uint32 SendCheckBW();

    Uint32 ParseHandeSharkeS0S1(const Uint8* pBuffer, Uint32 nLen, Uint32& nUsed);
    Uint32 ParseHandeSharkeS2(const Uint8* pBuffer, Uint32 nLen, Uint32& nUsed);
    Uint32 OnCommandRequest(const MkString& Command, const MkVector<MkAmfObject>& VecObj) override;
private:
    MkString        m_LocalSig;
    MkString        m_RemoteSig;
    Uint32          m_RtmpState;
    //Uint8*          m_pBuffer;
    //Uint32          m_nLen;
    //Uint32          m_nOffset;

    MkString                m_MediaData;
    MkString                m_AppName;
    MkString                m_Path;
    MkString                m_TcUrl;
    MkRtmpProtocol          m_Protocol;
    MkString                m_StreamType;
    Uint32                  m_RtmpPlayFlags;
    Uint32                  m_SeekTime;
    Uint32                  m_StopTime;

    Uint32                  m_AudioSupports;
    Uint32                  m_VideoSupports;
    Uint32                  m_VideoFunction;
    MkString                m_UserToken;
    Uint32                  m_nBWCheckCounter;
};


#endif